<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Demo</title>
    <!-- 请勿在项目正式环境中引用该 layui.css 地址 -->
    <link rel="stylesheet" href="resource/js/layuimini-v2/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="resource/js/layuimini-v2/css/layuimini.css?v=2.0.4.2" media="all">
</head>
<body>
<table class="layui-hide" id="ID-table-demo-data" lay-filter="currentTableFilter"></table>

<!-- 请勿在项目正式环境中引用该 layui.js 地址 -->
<script src="resource/js/layuimini-v2/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="resource/js/layuimini-v2/js/lay-config.js?v=2.0.0" charset="utf-8"></script>
<script type="text/html" id="toolbarDemo">
    {{if eq .linkSuccess true}}
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" linkId="{{.linkId}}" lay-event="add"> 新增库 </button>
        <button class="layui-btn layui-btn-normal layui-btn-sm data-edit-btn" linkId="{{.linkId}}" lay-event="edit"> 编辑连接 </button>
        <button class="layui-btn layui-btn-normal layui-btn-sm data-del-btn" linkId="{{.linkId}}" lay-event="del"> 删除连接 </button>
    </div>
    {{else}}
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-normal layui-btn-sm data-edit-btn" linkId="{{.linkId}}" lay-event="edit"> 编辑连接 </button>
        <button class="layui-btn layui-btn-normal layui-btn-sm data-del-btn" linkId="{{.linkId}}" lay-event="del"> 删除连接 </button>
    </div>
    {{end}}
</script>
<script type="text/html" id="currentTableBar">
    <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="stable">超级表</a>
    <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="table">表</a>
    <a class="layui-btn layui-btn-normal layui-btn-xs data-count-ddl" lay-event="ddl">DDL</a>
    <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
</script>
<script>
    layui.use(['jquery','table','layer','miniTab'], function(){
        var $ = layui.jquery,
            table = layui.table,
            layer = layui.layer,
            miniTab = layui.miniTab;
        //miniTab.listen();

        var linkId = '{{.linkId}}'
        var linkSuccess = '{{.linkSuccess}}'
        if(linkSuccess == 'false'){
            layer.msg('连接数据库失败,请检查连接信息');
        }
        var data = [];
        {{range $index, $elem := .linkDb}}
        var menuInfo ={
            "dbName": '{{$elem}}',
        }
        data.push(menuInfo)
        {{end}}

        // 已知数据渲染
        var inst = table.render({
            elem: '#ID-table-demo-data',
            toolbar: '#toolbarDemo',
            cols: [[ //标题栏
                {field: 'dbName', title: '数据库', width: 400,},
                {title: '操作', toolbar: '#currentTableBar', align: "left"}
            ]],
            data: data,
            //skin: 'line', // 表格风格
            //even: true,
            page: false, // 是否显示分页
            limits: [5, 10, 15],
            limit: 5 // 每页默认显示的数量
        });


        /**
         * toolbar监听事件
         */
        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            var tr = obj.tr; // 获取当前行的DOM对象
            if (obj.event === 'table') {
                miniTab.openTdNewTabByIframe({
                    href:"table?linkId="+linkId+"&dbName="+data.dbName+"&sp=false",
                    title:data.dbName+"/普通表",
                });

            }else if (obj.event === 'stable') {
                miniTab.openTdNewTabByIframe({
                    href:"table?linkId="+linkId+"&dbName="+data.dbName+"&sp=true",
                    title:data.dbName+"/超级表",
                });

            } else if (obj.event === 'delete') {
                layer.confirm('确认删除'+data.dbName+"?", function (index) {
                    var delData = {
                        linkId:linkId,
                        dbName:data.dbName,
                    }
                    $.ajax({
                        url:"/td/db/del/post",
                        type:"POST",
                        data:delData,
                        dataType : 'json',
                        async:false,
                        success:function (res) {
                            console.info(res)
                            if(res.code == 0){
                                obj.del();
                            }
                        }
                    });

                    layer.close(index);
                });
            }else if(obj.event === 'ddl'){
                var ddlData = {
                    linkId:linkId,
                    dbName:data.dbName,
                }
                $.ajax({
                    url:"/td/db/ddl/post",
                    type:"POST",
                    data:ddlData,
                    dataType : 'json',
                    async:false,
                    success:function (res) {
                        console.info(res)
                        if(res.code == 0){
                            var ddl = res.data.result;
                            var ddlHtml = '<div>'+ddl+'</div>';
                            layer.open({
                                type: 1,
                                title: 'DDL',
                                content: ddlHtml
                            })
                        }

                    }
                });
            }
        });


        $(".data-del-btn").on("click",function(){
            var linkId = $(this).attr('linkId');
            layer.confirm('确认删除该连接吗?', function (index) {
                var data = {
                    linkId:linkId
                }
                $.ajax({
                    url:"/td/link/del",
                    type:"POST",
                    data:data,
                    dataType : 'json',
                    success:function (res) {
                        console.info(res)
                        if(res.code == 0){
                            // 关闭当前iframe
                            var index = parent.layer.getFrameIndex(window.name); // 获取父页面的iframe层索引
                            parent.layer.close(index); // 关闭层
                            parent.location.href = "index"

                        }else{
                            layer.msg(res.msg);
                        }
                    },
                    error:function(){

                    }
                });

            });

        });

        $(".data-add-btn").on("click",function (){
            var linkId = $(this).attr('linkId');
            layer.open({
                type: 2,
                title: '新增库',
                shadeClose: true,
                shade: false,
                maxmin: false, //开启最大化最小化按钮
                area: ['100%', '100%'],
                btn: ['创建','取消'],
                content: 'createdb?linkId='+linkId,
                yes:function (index,layero){
                    console.info("---确认"+index);
                    var obj = $(layero).find("iframe")[0].contentWindow;
                    obj.save();
                    setTimeout(reload,500);
                    layer.close(index)
                },
                no:function (index,layero){
                    console.info("---取消"+index);
                },
                cancel: function(){

                }
            });
        });

        function reload(){
            parent.location.reload();
        }

    });
</script>

</body>
</html>